<?php

namespace common\models;

use Yii;
use \common\models\base\Region as BaseRegion;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "ims_region".
 */
class Region extends BaseRegion
{

    public function behaviors()
    {
        return ArrayHelper::merge(
            parent::behaviors(),
            [
                # custom behaviors
            ]
        );
    }

    public function rules()
    {
        return ArrayHelper::merge(
            parent::rules(),
            [
                # custom validation rules
            ]
        );
    }
    public static function getRegion($parentId=0)
    {
        $result = static::find()->where(['parent_id'=>$parentId])->asArray()->all();
        return ArrayHelper::map($result, 'id', 'name');
    }
    public static function getCityName($id=1){
        $result = static::find()->where(['id'=>$id])->select('name')->asArray()->one();
        return $result['name'];
    }
    public static function getCityId($name){
        $result = static::find()->where(['name'=>$name])->select('id')->asArray()->one();
        return $result['id'];
    }
    //为防止地区重名，用名字+父级id来获取某个名字下的地区id
    public static function getCity($name,$id){
        $result = static::find()->where(['name'=>$name,'parent_id'=>$id])->select('id')->asArray()->one();
        return $result['id'];
    }
}
